package main

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type StuDb struct {
	Id   int
	Name string
	Age  int
}

func (s *StuDb) TableName() string {
	return "stu"
}

var db *gorm.DB

func main() {
	dsn := "root:root@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
	var err error
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println(err)
	}
	// read()
	update()
}

func read() {
	s2 := StuDb{}
	ss := make([]StuDb, 0, 10)
	db.First(&s2, 1)
	fmt.Println(s2)
	s3 := StuDb{}
	db.First(&s3, "age=?", 16)
	fmt.Println(s3)
	// db.Take(&s2)
	// db.Last(&s2)
	// db.Find(&ss, []int{5, 7})
	fmt.Println(ss)
}

func create() {
	// s1 := StuDb{Name: "kkkk", Age: 19}
	// db.Create(&s1)
	// fmt.Println(s1.Id)

	db.Model(&StuDb{}).Create(map[string]interface{}{
		"name": "yeax",
		"age":  15,
	})

}

func update() {
	s1 := new(StuDb)
	db.First(&s1, 2)
	db.Model(&s1).Update("name", "hello")

}
